/*************************************************************************
 *                                                                       *
 * This file is part of the DogosFrame distribution.  For full copyright *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 *                                                                       *
 *************************************************************************/

//declaration header
#include "COMMON/tbox/IEEE.h"

namespace DOGOS
{

/*
 *************************************************************************
 * Set up the IEEE exception handlers so that normal IEEE exceptions
 * will cause a program abort.  How this is done varies wildly from
 * architecture to architecture.
 *************************************************************************
 */

/*
 * Function called when an exception is tripped.
 */
#if defined(HAVE_EXCEPTION_HANDLING)
static void
error_action(
    int error)
{
    Utilities::abort(
        "Floating point exception -- program abort! "
        + Utilities::int_to_string(error),
        __FILE__,
        __LINE__);
}
#endif

void
IEEE::setup_floating_point_exception_handlers()
{
#if defined(HAVE_EXCEPTION_HANDLING)
    int fpu_flags = _FPU_DEFAULT;
    fpu_flags &= ~_FPU_MASK_IM;  /* Execption on Invalid operation */
    fpu_flags &= ~_FPU_MASK_ZM;  /* Execption on Division by zero  */
    fpu_flags &= ~_FPU_MASK_OM;  /* Execption on Overflow */
    _FPU_SETCW(fpu_flags);
    signal(SIGFPE, error_action);
#endif
}

}
